package com.hfb.mashibing.alip8.spring.config;

import org.springframework.amqp.core.Binding;
import org.springframework.amqp.core.BindingBuilder;
import org.springframework.amqp.core.Exchange;
import org.springframework.amqp.core.ExchangeBuilder;
import org.springframework.amqp.core.Queue;
import org.springframework.amqp.core.QueueBuilder;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
public class RabbitMQConfig {
    // 交换机名称
    public static final String RABBITMQ_TOPIC_EXCHANGE = "item_rabbitmq_topic_exchange";
    // 队列名称
    public static final String RABIITMA_QUEUE_NAME = "item_queue";

    // 声明交换机
    @Bean("itemRabiitmqTopicExchange")
    public Exchange topicExchange(){
        return ExchangeBuilder.topicExchange(RABBITMQ_TOPIC_EXCHANGE).durable(true).build();
    }
    // 声明队列
    @Bean("itemQueue")
    public Queue getQueue(){
        return QueueBuilder.durable(RABIITMA_QUEUE_NAME).build();
    }
    // 将队列绑定到交换机
    @Bean
    public Binding queueBindingExchange(@Qualifier("itemQueue") Queue queue,
                                        @Qualifier("itemRabiitmqTopicExchange") Exchange exchange){
        return BindingBuilder.bind(queue).to(exchange).with("item.#").noargs();
    }

}
